package com.carkeeper.user.controller;


import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.pojo.Employee;
import com.carkeeper.user.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询所有员工用户表里的员工
     * @return
     */
    @GetMapping("/findAllByPage/{currentPage}/{pageSize}")
    @ResponseBody
    public Result findAllByPage(@PathVariable("pageSize") int pageSize,@PathVariable("currentPage") int currentPage){
        //开启分页插件
        PageHelper.startPage(currentPage,pageSize);
        //查询所有员工信息
         List<Employee> employeeList =employeeService.findAll();
         //分页
        Page<Employee> employeePage= (Page<Employee>) employeeList;
      return new Result(true,StatusCode.OK,"分页查询成功",employeePage);
    }

    /**
     * 增加员工
     * @param employee
     *
     */
    @PostMapping
    @ResponseBody
  public Result add(@RequestBody Employee employee){
      employeeService.add(employee);
      return new Result(true,StatusCode.OK,"添加员工成功");
    }

    /***
     * 根据ID查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public Result findById(@PathVariable Integer id){
        Employee employee=employeeService.findById(id);
        return new Result(true,StatusCode.OK,"查询员工成功");
    }

    //更新员工信息
    @PutMapping("/{id}")
    @ResponseBody
    public Result update(@RequestBody Employee employee,@PathVariable Integer id){
        employee.setId(id);
        employeeService.update(employee);
        return new Result(true,StatusCode.OK,"更新员工信息成功");
    }

    //锁定员工账号
    @PutMapping("/lock/{id}")
    @ResponseBody
    public Result lockAccount(@PathVariable Integer id){

          return  employeeService.lock(id);
    }




    /**
     * 分页条件查询所有员工用户表里的员工
     * @return
     */
    @GetMapping("/searchByPage/{currentPage}/{pageSize}")
    @ResponseBody
    public Result searchByPage(@PathVariable("pageSize") int pageSize, @PathVariable("currentPage") int currentPage, @RequestParam Map<String,Object> searchMap){

        //开启分页插件
        PageHelper.startPage(currentPage,pageSize);
        //查询所有员工信息
        List<Employee> employeeList =employeeService.search(currentPage,pageSize,searchMap);


        //分页
        Page<Employee> employeePage= (Page<Employee>) employeeList;
        return new Result(true,StatusCode.OK,"分页条件查询成功",employeePage);
    }







}
